package code.web.controller;

import cn.hutool.core.util.StrUtil;
import code.service.IUserService;
import code.vo.PageVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import code.dto.ResultDto;
import code.model.User;

@Controller
@RequestMapping("user")
@Api(value = "用户相关接口", tags = {"用户相关接口"})
public class UserController {

    @Autowired
    private IUserService userService;

    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "添加一条记录接口")
    public ResultDto<User> addOne(@RequestBody User user) {
        User add = userService.add(user);
        if (add != null) {
            return ResultDto.ok(add);
        }
        return ResultDto.failed();
    }


    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "删除一条记录接口")
    public ResultDto<String> delete(@RequestParam @ApiParam(value = "主键") String id) {
        String delete = userService.delete(id);
        if (StrUtil.isNotBlank(delete)) {
            return ResultDto.ok(delete);
        }
        return ResultDto.failed();
    }


    @ResponseBody
    @RequestMapping(value = "/edit", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "修改记录接口")
    public ResultDto<User> edit(@RequestBody User edit) {
        User user = userService.edit(edit);
        if (user != null) {
            return ResultDto.ok(user);
        }
        return ResultDto.failed();
    }


    @ResponseBody
    @RequestMapping(value = "/detail", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "查询记录接口")
    public ResultDto<User> findOne(@RequestParam @ApiParam(value = "主键") String id) {
        User one = userService.findOne(id);
        if (one != null) {
            return ResultDto.ok(one);
        }
        return ResultDto.failed();
    }


    /**
     * #@RequestParam的意思是请求参数，不管是GET还是POST参数都可以接收到。
     * <p>
     * #*如果是GET方法,可以使用RequestParam来拼装参数到对象上,不管是GET还是POST参数都可以接收到*
     * <p>
     * #@RequestParam(required = false)
     * <p>
     *
     * @param userName   用户名称
     * @param pageNumber 页码
     * @param pageSize   分页大小
     * @return 结果
     * @see <a href="https://blog.csdn.net/w605283073/article/details/86653699">GET请求使用@RequestBody的正确姿势</a>
     */

    @ResponseBody
    @RequestMapping(value = "/page", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "分页条件查询记录接口")
    public ResultDto<PageVo<User>> findPage(
            @RequestParam(required = false) String userName
            , @RequestParam(required = false, defaultValue = "1") int pageNumber
            , @RequestParam(required = false, defaultValue = "10") int pageSize
    ) {
        PageVo<User> page = userService.findPage(userName, pageNumber, pageSize);
        return ResultDto.ok(page);
    }

}
